package com.mdd.admin.service.setting.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.setting.IDevNoticeSettingService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.setting.DevNoticeSettingParam;
import com.mdd.admin.vo.setting.DevNoticeSettingListVo;
import com.mdd.admin.vo.setting.DevNoticeSettingDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.setting.DevNoticeSetting;
import com.mdd.common.mapper.setting.DevNoticeSettingMapper;
import com.mdd.common.utils.ArrayUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.common.config.GlobalConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 通知设置实现类
 */
@Service
public class DevNoticeSettingServiceImpl implements IDevNoticeSettingService {
        
    @Resource
    DevNoticeSettingMapper devNoticeSettingMapper;

    /**
     * 通知设置列表
     *
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<DevNoticeSettingListVo>
     */
    @Override
    public PageResult<DevNoticeSettingListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page  = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        QueryWrapper<DevNoticeSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        devNoticeSettingMapper.setSearch(queryWrapper, params, new String[]{
            "=:sceneId@scene_id:int",
            "like:sceneName@scene_name:str",
            "=:sceneDesc@scene_desc:str",
            "=:recipient:int",
            "=:type:int",
            "=:systemNotice@system_notice:str",
            "=:smsNotice@sms_notice:str",
            "=:oaNotice@oa_notice:str",
            "=:mnpNotice@mnp_notice:str",
            "=:support:str",
        });

        IPage<DevNoticeSetting> iPage = devNoticeSettingMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<DevNoticeSettingListVo> list = new LinkedList<>();
        for(DevNoticeSetting item : iPage.getRecords()) {
            DevNoticeSettingListVo vo = new DevNoticeSettingListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 通知设置详情
     *
     * @param id 主键参数
     * @return DevNoticeSetting
     */
    @Override
    public DevNoticeSettingDetailVo detail(Integer id) {
        DevNoticeSetting model = devNoticeSettingMapper.selectOne(
                new QueryWrapper<DevNoticeSetting>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        DevNoticeSettingDetailVo vo = new DevNoticeSettingDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 通知设置新增
     *
     * @param devNoticeSettingParam 参数
     */
    @Override
    public void add(DevNoticeSettingParam devNoticeSettingParam) {
        DevNoticeSetting model = new DevNoticeSetting();
        model.setSceneId(devNoticeSettingParam.getSceneId());
        model.setSceneName(devNoticeSettingParam.getSceneName());
        model.setSceneDesc(devNoticeSettingParam.getSceneDesc());
        model.setRecipient(devNoticeSettingParam.getRecipient());
        model.setType(devNoticeSettingParam.getType());
        model.setSystemNotice(devNoticeSettingParam.getSystemNotice());
        model.setSmsNotice(devNoticeSettingParam.getSmsNotice());
        model.setOaNotice(devNoticeSettingParam.getOaNotice());
        model.setMnpNotice(devNoticeSettingParam.getMnpNotice());
        model.setSupport(devNoticeSettingParam.getSupport());
        devNoticeSettingMapper.insert(model);
    }

    /**
     * 通知设置编辑
     *
     * @param devNoticeSettingParam 参数
     */
    @Override
    public void edit(DevNoticeSettingParam devNoticeSettingParam) {
        DevNoticeSetting model = devNoticeSettingMapper.selectOne(
                new QueryWrapper<DevNoticeSetting>()
                    .eq("id",  devNoticeSettingParam.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(devNoticeSettingParam.getId());
        model.setSceneId(devNoticeSettingParam.getSceneId());
        model.setSceneName(devNoticeSettingParam.getSceneName());
        model.setSceneDesc(devNoticeSettingParam.getSceneDesc());
        model.setRecipient(devNoticeSettingParam.getRecipient());
        model.setType(devNoticeSettingParam.getType());
        model.setSystemNotice(devNoticeSettingParam.getSystemNotice());
        model.setSmsNotice(devNoticeSettingParam.getSmsNotice());
        model.setOaNotice(devNoticeSettingParam.getOaNotice());
        model.setMnpNotice(devNoticeSettingParam.getMnpNotice());
        model.setSupport(devNoticeSettingParam.getSupport());
        devNoticeSettingMapper.updateById(model);
    }

    /**
     * 通知设置删除
     *
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        DevNoticeSetting model = devNoticeSettingMapper.selectOne(
                new QueryWrapper<DevNoticeSetting>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        devNoticeSettingMapper.delete(new QueryWrapper<DevNoticeSetting>().eq("id", id));
    }

}
